<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <div>
        <el-row :gutter="20">
          <el-col :span="8">
            <!-- 搜索框 -->
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getList">
              <el-button slot="append" icon="el-icon-search" @click="getList"></el-button>
            </el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="addDialogVisible = true;">添加角色</el-button>
          </el-col>
        </el-row>
        <el-table
          :data="roleList"
          border
          stripe
        >
          <el-table-column type="index"></el-table-column>
          <el-table-column
            prop="roleName"
            label="角色名"
          >
          </el-table-column>
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-tooltip effect="dark" content="修改角色名称" placement="top" :enterable="false">
                <el-button type="primary" icon="el-icon-edit" size="mini"
                           @click="fillUpdateForm(scope.row);updateDialogVisible = true"></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" content="删除该角色" placement="top" :enterable="false">
                <el-button type="danger" icon="el-icon-delete" size="mini"
                           @click="deleteRole(scope.row.id)"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[5, 10, 15]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>

        <el-dialog
          title="添加账户"
          :visible.sync="addDialogVisible"
          width="50%">
          <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="80px">
            <el-form-item prop="roleName" label="角色名称">
              <el-input v-model="addForm.roleName" clearable></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addRole">确 定</el-button>
          </span>
        </el-dialog>
        <el-dialog
          title="修改角色名"
          :visible.sync="updateDialogVisible"
          width="50%">
          <el-form ref="updateFormRef" :model="updateForm" :rules="updateFormRules" label-width="80px">
            <el-form-item prop="typeName" label="角色名">
              <el-input v-model="updateForm.roleName" clearable></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="updateDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateRole">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </el-card>

  </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        pageNum: 1,
        pageSize: 5,
        query: ''
      },
      total: 0,
      addDialogVisible: false,
      updateDialogVisible: false,
      updateFormRules: {},
      updateForm: {
        id: 0,
        roleName: ''
      },
      roleList: [],
      addForm: {
        roleName: ''
      },
      addFormRules: {
        roleName: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: 'blur'
          }
        ]
      },
      manageDialogVisible: false,
      manageForm: {},
      props: {
        label: 'name',
        children: 'zones'
      },
      count: 1
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const { data: res } = await this.$http.get('/role', { params: this.queryInfo })
      if (res.code !== 200) {
        this.$message.error(res.message)
        return
      }
      this.roleList = res.data.roles.records
      this.total = res.data.roles.total
    },
    addRole () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/role', this.addForm)
        if (res.code === 201) {
          this.$message.success(res.message)
          this.addDialogVisible = false
          await this.getList()
        } else {
          console.log(res)
          this.$message.error('添加账户失败')
        }
      })
    },
    deleteRole (id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete('/role', { data: { id: id } })
        if (res.code === 200) {
          this.$message.success('删除角色成功')
          await this.getList()
        } else {
          this.$message.error('删除角色失败')
          console.log(res)
        }
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    updateRole () {
      this.$confirm('此操作将修改该角色名称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.put('/role', this.updateForm)
        if (res.code === 200) {
          this.$message.success('修改角色名称成功')
          this.updateDialogVisible = false
          await this.getList()
        } else {
          console.log(res)
          this.$message.error('修改角色名称失败')
        }
      }).catch(() => {
        this.$message.info('已取消')
      })
    },
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pageNum = newPage
      this.getList()
    },
    fillUpdateForm (info) {
      this.updateForm.id = info.id
      this.updateForm.roleName = info.roleName
    }
  }
}
</script>

<style lang="less" scoped>

</style>
